<template>
  <div class='container'>
    <el-dialog :title="isEdit ? '修改标签':'新增标签' " width="400px" :visible="showDialog" @open="open" @close="close">
     <el-form :model="form" :rules="rules" ref="ruleForm">
    <el-form-item label="所属学科" label-width="100px" prop="subjectID">
      <el-select v-model="form.subjectID" placeholder="请选择" style="width: 260px;">
        <el-option v-for="item in discipline" :key="item.value" :label="item.label"  :value="item.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="目录名称" label-width="100px"  prop="tagName">
      <el-input v-model="form.tagName" placeholder="请选择目录名称"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="close">取消</el-button>
    <el-button type="primary" @click="submitForm">确定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { add, update, detail } from '@/api/hmmm/tags.js'
export default {
  name: 'tagsAdd',
  data () {
    return {
      editList: {},
      form: {
        subjectID: '',
        tagName: ''
      },
      discipline: [],
      rules: {
        subjectID: [{ required: true, message: '请输入目录名称', trigger: ['blur', 'change'] }],
        tagName: [{ required: true, message: '请输入目录名称', trigger: ['blur', 'change'] }]
      }
    }
  },
  props: {
    showDialog: {
      type: Boolean
    },
    isEdit: {
      typeof: Boolean
    },
    id: {
      typeof: Number
    }
  },
  created () {
    this.getDisciplineList()
  },
  methods: {
    // 学科简单列表
    async getDisciplineList () {
      const data = await simple()
      this.discipline = data.data
    },
    // 打开回调
    async open () {
      if (this.isEdit) {
        const { data } = await detail({ id: this.id })
        this.form.subjectID = data.subjectID
        this.form.tagName = data.tagName
      }
    },
    // 关闭弹窗
    close () {
      this.$emit('update:showDialog', false)
      this.$emit('update:isEdit', false)
      this.$refs.ruleForm.resetFields()
    },
    // 确认按钮
    submitForm () {
      // 规则校验成功发送添加axios
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            await update({ ...this.form, id: this.id })
            this.$message({
              message: '标签修改成功',
              type: 'success'
            })
          } else {
            await add(this.form)
            this.$message({
              message: '标签添加成功',
              type: 'success'
            })
          }
          this.close()
          this.$emit('addTableList')
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
